﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace CustomControls
{
    public partial class LabelMultiLine : TextBox
    {
        Form _ParentForm = null;

        //Kéo thả thì tự động nó biết ParentForm
        public LabelMultiLine()
        {
            InitializeComponent();
            this.ContextMenu = new ContextMenu();
            this.TextAlign = HorizontalAlignment.Left;
            this.TextChanged += new EventHandler(LabelMultiLine_TextChanged);
        }

        //Dùng khi khai báo bằng code , phải truyền vào ParentForm để set color
        public LabelMultiLine(Form parentForm)
        {
            InitializeComponent();
            this.ContextMenu = new ContextMenu();
            this.TextAlign = HorizontalAlignment.Right;
            this.TextChanged += new EventHandler(LabelMultiLine_TextChanged);
            _ParentForm = parentForm;
        }

        void LabelMultiLine_TextChanged(object sender, EventArgs e)
        {
            this.Multiline = true;
            if(this.Text != "")
            {
                //Kiểm tra xem Parent Form có sẵn sàng chưa
                Form frmParent = null;
                if (_ParentForm == null)
                    frmParent = GetParentForm(this);
                else
                    frmParent = _ParentForm;

                if (frmParent != null)
                {
                    this.BackColor = frmParent.BackColor;
                    this.BorderStyle = BorderStyle.None;
                }
                //Auto Size cho FOrm
                System.Drawing.SizeF mySize = new System.Drawing.SizeF();

                // Use the textbox font
                System.Drawing.Font myFont = this.Font;

                using (Graphics g = this.CreateGraphics())
                {
                    // Get the size given the string and the font
                    mySize = g.MeasureString(this.Text, myFont);
                }

                // Resize the textbox 
                this.Width = (int)Math.Round(mySize.Width, 0);
                this.Height = (int)Math.Round(mySize.Height, 0);
            }
            this.ReadOnly = false;
            this.TextAlign = HorizontalAlignment.Left;
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            this.ContextMenu = new ContextMenu();
            this.TextAlign = HorizontalAlignment.Left;
            this.BorderStyle = BorderStyle.Fixed3D;
        }

		[Description("Sets the numeric type allowed"), Category("Behavior")]

		public override string Text
		{
			get{return base.Text;}
			set
			{
				base.Text = value;
			}
		}

		protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
		{
			// trap Ctrl-V paste and prevent invalid values
			//	return false to allow further processing
			if(keyData == (Keys)Shortcut.CtrlV || keyData == (Keys)Shortcut.ShiftIns)
			{
				IDataObject iData = Clipboard.GetDataObject();
 
				// assemble new string and check IsValid
				string newText;
				newText = base.Text.Substring(0, base.SelectionStart)
					+ (string)iData.GetData(DataFormats.Text)
					+ base.Text.Substring(base.SelectionStart + base.SelectionLength);
			}
           
			return base.ProcessCmdKey(ref msg, keyData);
		}

		protected override void OnLeave(EventArgs e)
		{
			
		}

		protected override void OnKeyPress(KeyPressEventArgs e)
		{
			// assemble new text with new KeyStroke
			//	and pass to validation routine.

			// NOTES;
			//	1) Delete key is NOT passed here
			//	2) control passed here after ProcessCmdKey() is run

			char c = e.KeyChar;
			if(!Char.IsControl(c))	// not sure about this?? nothing in docs about what is Control char??
			{
				// prevent spaces
				if(c.ToString() == " ")
				{
					e.Handled = true;
					return;
				}

				string newText = base.Text.Substring(0, base.SelectionStart)
					+ c.ToString() + base.Text.Substring(base.SelectionStart + base.SelectionLength);
			}
           
			base.OnKeyPress(e);
		}

        private Form GetParentForm(Control parent)
        {
            Form form = parent as Form;
            if (form != null)
            {
                return form;
            }
            if (parent != null)
            {
                // Walk up the control hierarchy
                return GetParentForm(parent.Parent);
            }
            return null; // Control is not on a Form
        }
    }
}
